﻿using DingVision.Axis.Models;
using DingVision.Axis.Views;
using DingVision.Infrastructure.Events;
using DingVision.Infrastructure.Helpers;
using DingVision.Infrastructure.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace DingVision.Axis.ViewModels
{
    public class JogPageViewModel : BindableBase
    {
        #region 变量
        private DmcCard.AxisParm axisParm;
        double _speed = 100;
        private readonly IDialogService dialogService;
        private readonly IEventAggregator eventAggregator;
        private readonly IRegionManager regionManager;
        #endregion
        #region 属性绑定
        private int motionJogSelectedIndex = -1;
        public int MotionJogSelectedIndex
        {
            get { return motionJogSelectedIndex; }
            set { SetProperty(ref motionJogSelectedIndex, value); }
        }
        private bool servoState;
        public bool ServoState
        {
            get { return servoState; }
            set { SetProperty(ref servoState, value); }
        }
        private bool zeroState;
        public bool ZeroState
        {
            get { return zeroState; }
            set { SetProperty(ref zeroState, value); }
        }
        private bool alarmState;
        public bool AlarmState
        {
            get { return alarmState; }
            set { SetProperty(ref alarmState, value); }
        }
        private bool runningState;
        public bool RunningState
        {
            get { return runningState; }
            set { SetProperty(ref runningState, value); }
        }
        private bool pLimitState;
        public bool PLimitState
        {
            get { return pLimitState; }
            set { SetProperty(ref pLimitState, value); }
        }
        private bool nLimitState;
        public bool NLimitState
        {
            get { return nLimitState; }
            set { SetProperty(ref nLimitState, value); }
        }
        private bool _btnXBackwardIsEnabled;
        public bool BtnXBackwardIsEnabled
        {
            get { return _btnXBackwardIsEnabled; }
            set { SetProperty(ref _btnXBackwardIsEnabled, value); }
        }
        private bool _btnXForwardIsEnabled;
        public bool BtnXForwardIsEnabled
        {
            get { return _btnXForwardIsEnabled; }
            set { SetProperty(ref _btnXForwardIsEnabled, value); }
        }
        private bool _btnYBackwardIsEnabled;
        public bool BtnYBackwardIsEnabled
        {
            get { return _btnYBackwardIsEnabled; }
            set { SetProperty(ref _btnYBackwardIsEnabled, value); }
        }
        private bool _btnYForwardIsEnabled;
        public bool BtnYForwardIsEnabled
        {
            get { return _btnYForwardIsEnabled; }
            set { SetProperty(ref _btnYForwardIsEnabled, value); }
        }
        private bool _btnZBackwardIsEnabled;
        public bool BtnZBackwardIsEnabled
        {
            get { return _btnZBackwardIsEnabled; }
            set { SetProperty(ref _btnZBackwardIsEnabled, value); }
        }
        private bool _btnZForwardIsEnabled;
        public bool BtnZForwardIsEnabled
        {
            get { return _btnZForwardIsEnabled; }
            set { SetProperty(ref _btnZForwardIsEnabled, value); }
        }
        private bool _btnUBackwardIsEnabled;
        public bool BtnUBackwardIsEnabled
        {
            get { return _btnUBackwardIsEnabled; }
            set { SetProperty(ref _btnUBackwardIsEnabled, value); }
        }
        private bool _btnUForwardIsEnabled;
        public bool BtnUForwardIsEnabled
        {
            get { return _btnUForwardIsEnabled; }
            set { SetProperty(ref _btnUForwardIsEnabled, value); }
        }
        private double prfPos;
        public double PrfPos
        {
            get { return prfPos; }
            set { SetProperty(ref prfPos, value); }
        }
        private double encPos;
        public double EncPos
        {
            get { return encPos; }
            set { SetProperty(ref encPos, value); }
        }
        private double pTPSpeed = 100;
        public double PTPSpeed
        {
            get { return pTPSpeed; }
            set { SetProperty(ref pTPSpeed, value); }
        }
        private double pTPDis = 10;
        public double PTPDis
        {
            get { return pTPDis; }
            set { SetProperty(ref pTPDis, value); }
        }
        private ObservableCollection<RobotPoint> robotPoints = new ObservableCollection<RobotPoint>();
        public ObservableCollection<RobotPoint> RobotPoints
        {
            get { return robotPoints; }
            set { SetProperty(ref robotPoints, value); }
        }
        private ObservableCollection<RobotPoint> movePoints = new ObservableCollection<RobotPoint>();
        public ObservableCollection<RobotPoint> MovePoints
        {
            get { return movePoints; }
            set { SetProperty(ref movePoints, value); }
        }
        private int techPointSelectedIndex = 0;
        public int TechPointSelectedIndex
        {
            get { return techPointSelectedIndex; }
            set { SetProperty(ref techPointSelectedIndex, value); }
        }
        private int moveFuncSelectedIndex = 0;
        public int MoveFuncSelectedIndex
        {
            get { return moveFuncSelectedIndex; }
            set { SetProperty(ref moveFuncSelectedIndex, value); }
        }
        private int movePointSelectedIndex = 0;
        public int MovePointSelectedIndex
        {
            get { return movePointSelectedIndex; }
            set { SetProperty(ref movePointSelectedIndex, value); }
        }
        private double moveLimZ;
        public double MoveLimZ
        {
            get { return moveLimZ; }
            set { SetProperty(ref moveLimZ, value); }
        }
        private string moveAddZVisibility = "Collapsed";
        public string MoveAddZVisibility
        {
            get { return moveAddZVisibility; }
            set { SetProperty(ref moveAddZVisibility, value); }
        }
        private double moveAddZ;
        public double MoveAddZ
        {
            get { return moveAddZ; }
            set { SetProperty(ref moveAddZ, value); }
        }
        private string moveLimZVisibility = "Visible";
        public string MoveLimZVisibility
        {
            get { return moveLimZVisibility; }
            set { SetProperty(ref moveLimZVisibility, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand motionJogSelectionChangedEventCommand;
        public DelegateCommand MotionJogSelectionChangedEventCommand =>
            motionJogSelectionChangedEventCommand ?? (motionJogSelectionChangedEventCommand = new DelegateCommand(ExecuteMotionJogSelectionChangedEventCommand));
        private DelegateCommand<object> servoOperateCommand;
        public DelegateCommand<object> ServoOperateCommand =>
            servoOperateCommand ?? (servoOperateCommand = new DelegateCommand<object>(ExecuteServoOperateCommand));
        private DelegateCommand jogP_MouseDownCommand;
        public DelegateCommand JogP_MouseDownCommand =>
            jogP_MouseDownCommand ?? (jogP_MouseDownCommand = new DelegateCommand(ExecuteJogP_MouseDownCommand));
        private DelegateCommand jogN_MouseDownCommand;
        public DelegateCommand JogN_MouseDownCommand =>
            jogN_MouseDownCommand ?? (jogN_MouseDownCommand = new DelegateCommand(ExecuteJogN_MouseDownCommand));
        private DelegateCommand _techPointCommand;        
        public DelegateCommand TechPointCommand =>
            _techPointCommand ?? (_techPointCommand = new DelegateCommand(ExecuteTechPointCommand));
        private DelegateCommand movePointCommand;
        public DelegateCommand MovePointCommand =>
            movePointCommand ?? (movePointCommand = new DelegateCommand(ExecuteMovePointCommand));
        private DelegateCommand moveCMDSelectionChangedEventCommand;
        public DelegateCommand MoveCMDSelectionChangedEventCommand =>
            moveCMDSelectionChangedEventCommand ?? (moveCMDSelectionChangedEventCommand = new DelegateCommand(ExecuteMoveCMDSelectionChangedEventCommand));
        private DelegateCommand techPointCancelCommand;
        public DelegateCommand TechPointCancelCommand =>
            techPointCancelCommand ?? (techPointCancelCommand = new DelegateCommand(ExecuteTechPointCancelCommand));

        void ExecuteTechPointCancelCommand()
        {
            var region = regionManager.Regions["AxisContentRegion"];
            foreach (var view in region.Views)
            {
                region.Deactivate(view);
            }
            var view1 = (PointPage)region.Views.Where(t => t.GetType() == typeof(PointPage)).FirstOrDefault();
            region.Activate(view1);
        }
        void ExecuteMotionJogSelectionChangedEventCommand()
        {
            switch (MotionJogSelectedIndex)
            {
                case 2:
                    BtnXBackwardIsEnabled = false;
                    BtnXForwardIsEnabled = false;
                    BtnYBackwardIsEnabled = false;
                    BtnYForwardIsEnabled = false;
                    BtnZBackwardIsEnabled = true;
                    BtnZForwardIsEnabled = true;
                    BtnUBackwardIsEnabled = false;
                    BtnUForwardIsEnabled = false;
                    break;
                case 1:
                    BtnXBackwardIsEnabled = false;
                    BtnXForwardIsEnabled = false;
                    BtnYBackwardIsEnabled = true;
                    BtnYForwardIsEnabled = true;
                    BtnZBackwardIsEnabled = false;
                    BtnZForwardIsEnabled = false;
                    BtnUBackwardIsEnabled = false;
                    BtnUForwardIsEnabled = false;
                    break;

                case 3:
                    BtnXBackwardIsEnabled = false;
                    BtnXForwardIsEnabled = false;
                    BtnYBackwardIsEnabled = false;
                    BtnYForwardIsEnabled = false;
                    BtnZBackwardIsEnabled = false;
                    BtnZForwardIsEnabled = false;
                    BtnUBackwardIsEnabled = true;
                    BtnUForwardIsEnabled = true;
                    break;
                default:
                case 0:
                    BtnXBackwardIsEnabled = true;
                    BtnXForwardIsEnabled = true;
                    BtnYBackwardIsEnabled = false;
                    BtnYForwardIsEnabled = false;
                    BtnZBackwardIsEnabled = false;
                    BtnZForwardIsEnabled = false;
                    BtnUBackwardIsEnabled = false;
                    BtnUForwardIsEnabled = false;
                    break;
            }
            RefreshSts();
            GetAxis(MotionJogSelectedIndex);
        }
        void ExecuteServoOperateCommand(object obj)
        {
            if (MotionJogSelectedIndex == -1 && obj.ToString() != "4")
            {
                System.Windows.MessageBox.Show("请选择轴号");
                return;
            }
            switch (obj.ToString())
            {
                case "0":
                    {
                        DmcCard.AxisStatus axisStatus = DmcCard.Instance.GetAxisStatus(this.axisParm);
                        if (!axisStatus.FlagServoOn)
                        {
                            DmcCard.Instance.ServoOn(this.axisParm);
                            System.Threading.Thread.Sleep(500);
                            switch (MotionJogSelectedIndex)
                            {
                                case 0:
                                    DmcCard.Instance.PulsePos_Axis1 = DmcCard.Instance.EncPos_Axis1;
                                    break;
                                case 1:
                                    DmcCard.Instance.PulsePos_Axis2 = DmcCard.Instance.EncPos_Axis2;
                                    break;
                                case 2:
                                    DmcCard.Instance.PulsePos_Axis3 = DmcCard.Instance.EncPos_Axis3;
                                    break;
                                case 3:
                                    DmcCard.Instance.PulsePos_Axis4 = DmcCard.Instance.EncPos_Axis4;
                                    break;
                                case 4:
                                    DmcCard.Instance.PulsePos_Axis5 = DmcCard.Instance.EncPos_Axis5;
                                    break;
                                case 5:
                                    DmcCard.Instance.PulsePos_Axis6 = DmcCard.Instance.EncPos_Axis6;
                                    break;
                                case 6:
                                    DmcCard.Instance.PulsePos_Axis7 = DmcCard.Instance.EncPos_Axis7;
                                    break;
                                case 7:
                                    DmcCard.Instance.PulsePos_Axis8 = DmcCard.Instance.EncPos_Axis8;
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    break;
                case "1":
                    DmcCard.Instance.ServoOff(axisParm);
                    break;
                case "2":
                    DmcCard.Instance.ClearAlm(axisParm);
                    break;
                case "3":
                    DmcCard.Instance.SigAxisHomeMove(axisParm);
                    break;
                case "4":
                    _speed = PTPSpeed;
                    System.Windows.MessageBox.Show("修改成功");
                    break;
                case "5":
                    double giveDis = PTPDis;                    
                    DmcCard.Instance.AxisTrapMove(this.axisParm, giveDis, _speed);
                    break;
                default:
                    break;
            }
        }
        void ExecuteJogP_MouseDownCommand()
        {
            try
            {
                if (MotionJogSelectedIndex == -1)
                {
                    System.Windows.MessageBox.Show("请选择轴号");
                }
                else
                {
                    DmcCard.Instance.AxisJog(axisParm, 1, _speed);
                }
            }
            catch { }
        }
        void ExecuteJogN_MouseDownCommand()
        {
            try
            {
                if (MotionJogSelectedIndex == -1)
                {
                    System.Windows.MessageBox.Show("请选择轴号");
                }
                else
                {
                    DmcCard.Instance.AxisJog(axisParm, 0, _speed);
                }
            }
            catch { }
        }
        void ExecuteTechPointCommand()
        {
            if (RobotPoints[TechPointSelectedIndex].Isrecord == true)
            {
                if (MessageBox.Show($"你确定示教{RobotPoints[TechPointSelectedIndex].PIndex}吗？", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
                {

                    var x = DmcCard.Instance.EncPos_Axis1;
                    var y = DmcCard.Instance.EncPos_Axis2;
                    var z = DmcCard.Instance.EncPos_Axis3;
                    var r = DmcCard.Instance.EncPos_Axis4;
                    RobotPoints[TechPointSelectedIndex].X = x;
                    RobotPoints[TechPointSelectedIndex].Y = y;
                    RobotPoints[TechPointSelectedIndex].Z = z;
                    RobotPoints[TechPointSelectedIndex].R = r;

                    eventAggregator.GetEvent<AxisJogToPointEvent>().Publish(new RobotPointItem() { 
                        PIndex = RobotPoints[TechPointSelectedIndex].PIndex,
                        PName = RobotPoints[TechPointSelectedIndex].PName,
                        X = x,
                        Y = y,
                        Z = z,
                        R = r,
                    });
                }
            }
            else
            {
                if (MessageBox.Show($"你确定重新示教{RobotPoints[TechPointSelectedIndex].PIndex}吗？", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    DialogParameters param = new DialogParameters();
                    param.Add("PIndex", RobotPoints[TechPointSelectedIndex].PIndex);

                    dialogService.Show("TeachPointDialog", param, arg =>
                    {
                        if (arg.Result == ButtonResult.OK)
                        {
                            var PName = arg.Parameters.GetValue<string>("PName");
                            RobotPoints[TechPointSelectedIndex].PName = PName;
                            var x = DmcCard.Instance.EncPos_Axis1;
                            var y = DmcCard.Instance.EncPos_Axis2;
                            var z = DmcCard.Instance.EncPos_Axis3;
                            var r = DmcCard.Instance.EncPos_Axis4;
                            RobotPoints[TechPointSelectedIndex].X = x;
                            RobotPoints[TechPointSelectedIndex].Y = y;
                            RobotPoints[TechPointSelectedIndex].Z = z;
                            RobotPoints[TechPointSelectedIndex].R = r;
                            RobotPoints[TechPointSelectedIndex].Isrecord = true;

                            eventAggregator.GetEvent<AxisJogToPointEvent>().Publish(new RobotPointItem()
                            {
                                PIndex = RobotPoints[TechPointSelectedIndex].PIndex,
                                PName = PName,
                                X = x,
                                Y = y,
                                Z = z,
                                R = r,
                            });

                            MovePoints.Clear();
                            foreach (var item in RobotPoints)
                            {
                                if (item.Isrecord)
                                {
                                    MovePoints.Add(item);
                                }
                            }
                        }                        
                    });
                   

                }
            }
        }
        void ExecuteMovePointCommand()
        {
            string cmdmes = "";
            if (MovePointSelectedIndex < 0)
            {
                System.Windows.MessageBox.Show("请选择目标点位","错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            switch (MoveFuncSelectedIndex)
            {
                case 0:
                    cmdmes = $"Jump {MovePoints[MovePointSelectedIndex].PIndex} :Z({MoveLimZ}) LIMZ {MoveLimZ}";
                    break;
                case 1:
                    cmdmes = $"Jump {MovePoints[MovePointSelectedIndex].PIndex} +Z({MoveAddZ}) LIMZ {MoveLimZ}";
                    break;
                case 2:
                    cmdmes = $"Go {MovePoints[MovePointSelectedIndex].PIndex}";
                    break;
                default:
                    break;
            }
            if (MessageBox.Show($"准备执行命令{cmdmes}\r\n继续吗？", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                CancellationTokenSource source1 = new CancellationTokenSource();
                CancellationToken token1 = source1.Token;
                Task.Run(() => MovePointAction(MoveFuncSelectedIndex, MovePoints[MovePointSelectedIndex], MoveAddZ, MoveLimZ, token1));
                DialogParameters param = new DialogParameters();
                param.Add("CommandMsg", cmdmes);

                dialogService.Show("MovePointDialog", param, arg =>
                {
                    if (arg.Result != ButtonResult.OK)
                    {
                        //用户中断运动
                        source1.Cancel();
                        DmcCard.Instance.AxisStop(DmcCard.Instance.Axis1, 0);
                        DmcCard.Instance.AxisStop(DmcCard.Instance.Axis2, 0);
                        DmcCard.Instance.AxisStop(DmcCard.Instance.Axis3, 0);
                        DmcCard.Instance.AxisStop(DmcCard.Instance.Axis4, 0);
                    }
                });
            }
        }
        void ExecuteMoveCMDSelectionChangedEventCommand()
        {
            MoveAddZ = 0;
            MoveLimZ = 0;
            switch (MoveFuncSelectedIndex)
            {
                case 0:
                    MoveAddZVisibility = "Collapsed";
                    MoveLimZVisibility = "Visible";
                    break;
                case 1:
                    MoveAddZVisibility = "Visible";
                    MoveLimZVisibility = "Visible";
                    break;
                case 2:
                    MoveAddZVisibility = "Collapsed";
                    MoveLimZVisibility = "Collapsed";
                    break;
                default:
                    break;
            }
        }
      
        #endregion
        #region 构造函数
        public JogPageViewModel(IRegionManager _regionManager, IEventAggregator _eventAggregator, IDialogService _dialogService)
        {
            dialogService = _dialogService;
            eventAggregator = _eventAggregator;
            regionManager = _regionManager;
            eventAggregator.GetEvent<ShellSendEvent>().Subscribe(ShellSendEventReceived);
            eventAggregator.GetEvent<AxisPointUpdateAllPointsEvent>().Subscribe(AxisPointUpdateAllPointsEventReceived);
        }

   


        #endregion
        #region 自定义函数
        private void RefreshSts()
        {
            ServoState = false;
            ZeroState = false;
            AlarmState = false;
            RunningState = false;
            PLimitState = false;
            NLimitState = false;
        }
        private void GetAxis(int AxisNo)
        {
            switch (AxisNo)
            {
                case 0:
                    axisParm = DmcCard.Instance.Axis1;
                    return;
                case 1:
                    axisParm = DmcCard.Instance.Axis2;
                    return;
                case 2:
                    axisParm = DmcCard.Instance.Axis3;
                    return;
                case 3:
                    axisParm = DmcCard.Instance.Axis4;
                    return;
                case 4:
                    axisParm = DmcCard.Instance.Axis5;
                    return;
                case 5:
                    axisParm = DmcCard.Instance.Axis6;
                    return;
                case 6:
                    axisParm = DmcCard.Instance.Axis7;
                    return;
                case 7:
                    axisParm = DmcCard.Instance.Axis8;
                    return;
                default:
                    return;
            }
        }
        private void ShellSendEventReceived(string msg)
        {
            switch (msg)
            {
                case "Loaded":
                    try
                    {
                        RobotPoints = JsonHelp.JsonRead<ObservableCollection<RobotPoint>>(System.IO.Path.Combine(System.Environment.CurrentDirectory, "RobotPoints.json"));
                        TechPointSelectedIndex = 0;
                        MovePoints.Clear();
                        foreach (var item in RobotPoints)
                        {
                            if (item.Isrecord)
                            {
                                MovePoints.Add(item);
                            }
                        }
                        MovePointSelectedIndex = 0;
                    }
                    catch { }
                    Run();
                    break;
                default:
                    break;
            }
        }
        private void MovePointAction(int funMode,RobotPoint point,double z1,double limz, CancellationToken token)
        {
            int prmStep = 0;
            switch (funMode)
            {
                case 0:
                    prmStep = 0;//1:抬Z轴；2：水平移动
                    break;
                case 1:
                    prmStep = 100;//1:抬Z轴；2：水平移动；3：降Z轴
                    break;
                case 2:
                    prmStep = 200;//直接移动
                    break;
                default:
                    break;
            }
            while (true)
            {
                switch (prmStep)
                {
                    case 0:
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis3, limz,_speed);
                        prmStep = 1;
                        break;
                    case 1:
                        if (DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis3))
                        {
                            prmStep = 2;
                        }
                        break;
                    case 2:
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis1, point.X, _speed);
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis2, point.Y, _speed);
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis4, point.R, _speed);
                        prmStep = 3;
                        break;
                    case 3:
                        if (DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis1) && DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis2) && DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis3))
                        {
                            prmStep = 300;
                        }
                        break;

                    case 100:
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis3, limz, _speed);
                        prmStep = 101;
                        break;
                    case 101:
                        if (DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis3))
                        {
                            prmStep = 102;
                        }
                        break;
                    case 102:
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis1, point.X, _speed);
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis2, point.Y, _speed);
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis4, point.R, _speed);
                        prmStep = 103;
                        break;
                    case 103:
                        if (DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis1) && DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis2) && DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis4))
                        {
                            prmStep = 104;
                        }
                        break;
                    case 104:
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis3, point.Z + z1, _speed);
                        prmStep = 105;
                        break;
                    case 105:
                        if (DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis3))
                        {
                            prmStep = 300;
                        }
                        break;
                    case 200:
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis1, point.X, _speed);
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis2, point.Y, _speed);
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis3, point.Z, _speed);
                        DmcCard.Instance.AxisPosMove(DmcCard.Instance.Axis4, point.R, _speed);
                        prmStep = 201;
                        break;
                    case 201:
                        if (DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis1) && DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis2) && DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis3) && DmcCard.Instance.AxisCheckDone(DmcCard.Instance.Axis4))
                        {
                            prmStep = 300;
                        }
                        break;
                    case 300:
                        System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            eventAggregator.GetEvent<JogMovePointFinishEvent>().Publish();
                        }));
                        return;
                    default:
                        break;
                }
              
                if (token.IsCancellationRequested)
                {
                    return;
                }
                System.Threading.Thread.Sleep(100);
            }
        }
        private void AxisPointUpdateAllPointsEventReceived()
        {
            RobotPoints = JsonHelp.JsonRead<ObservableCollection<RobotPoint>>(System.IO.Path.Combine(System.Environment.CurrentDirectory, "RobotPoints.json"));
            TechPointSelectedIndex = 0;            
            MovePoints.Clear();
            foreach (var item in RobotPoints)
            {
                if (item.Isrecord)
                {
                    MovePoints.Add(item);
                }
            }
            MovePointSelectedIndex = 0;
        }
        #endregion
        #region 运行
        async void Run()
        {
            while (true)
            {
                try
                {
                    if (DmcCard.Instance.OpenCardOk)
                    {
                        DmcCard.AxisStatus axisStatus = DmcCard.Instance.GetAxisStatus(axisParm);
                        ZeroState = axisStatus.FlagHome;
                        RunningState = !axisStatus.FlagMoveEnd;
                        ServoState = axisStatus.FlagServoOn;
                        PLimitState = axisStatus.FlagPosLimit;
                        NLimitState = axisStatus.FlagNeglimit;
                        AlarmState = axisStatus.FlagAlm;

                        PrfPos = axisStatus.PrfPos;
                        EncPos = axisStatus.EncPos;
                    }
                }
                catch { }
                await Task.Delay(100);
            }

        }
        #endregion
    }
}
